import { CalcDimension, Dimension } from "../entity/CalcDimension";
import { DimensionUnit } from "../entity/Enum";
import { Length, LengthUnits } from "../entity/Length";

export abstract class UtilsBase {
    LessOrEqual(left: number, right: number): boolean {
        let epsilon = 0.001;
        return (left - right) < epsilon;
    }
    CheckJs4SideValue(jsValue: any) {
        let hasTop: boolean = false;
        let hasRight: boolean = false;
        let hasBottom: boolean = false;
        let hasLeft: boolean = false;

        if (jsValue['top'] !== undefined) {
            //值检测
            if (this.CheckJsLengthValue(jsValue['top'])) {
                hasTop = true;
            } else {
                console.log("offset: don't hasTop ");
            }
        }
        if (jsValue['right'] !== undefined) {
            //值检测
            if (this.CheckJsLengthValue(jsValue['right'],)) {
                hasRight = true;
            } else {
                console.log("offset: don't hasRight ");
            }
        }
        if (jsValue['bottom'] !== undefined) {
            //值检测
            if (this.CheckJsLengthValue(jsValue['bottom'],)) {
                hasBottom = true;
            } else {
                console.log("offset: don't hasBottom ");
            }
        }
        if (jsValue['left'] !== undefined) {
            //值检测
            if (this.CheckJsLengthValue(jsValue['left'],)) {
                hasLeft = true;
            } else {
                console.log("offset: don't hasLeft ");
            }
        }
        return hasTop || hasRight || hasBottom || hasLeft;
    }
    CheckJsPositionValue(jsValue: any) {
        let hasX: boolean = false;
        let hasY: boolean = false;

        if (jsValue['x'] !== undefined) {
            //值检测
            if (this.CheckJsLengthValue(jsValue['x'])) {
                hasX = true;
            } else {
                console.log("offset: don't hasX ");
            }
        }
        if (jsValue['y'] !== undefined) {
            //值检测
            if (this.CheckJsLengthValue(jsValue['y'],)) {
                hasY = true;
            } else {
                console.log("offset: don't hasY ");
            }
        }
        return hasX || hasY;
    }

    CheckJsLengthValue(jsValue: any) {
        if (typeof jsValue === 'number') {
            console.log('CheckJsLengthValue ' + ' is number ');
            return true;
        }

        if (typeof jsValue === 'string') {
            return this.StringToCalcDimension(jsValue);
        }

        if (typeof jsValue === 'object') {
            if (!jsValue.hasOwnProperty('id') || typeof jsValue.id !== 'number') {
                console.log('resId is not number');
                return false;
            }
            return true;
        }

        return false;
    }

    StringToCalcDimension(jsValue: string) {
        if (jsValue === "auto") {
            return true;
        }
        
        if (jsValue.startsWith('calc')) {
            return true;
        }
        if (!isNaN(Number(jsValue))) {
            return true;
        }
        let resultB = false;
        for (let u of LengthUnits) {
            //100px 
            let result = jsValue.split(u);
            //不含指定字符字符串
            if (result.length === 2 && result[1] === '' && !isNaN(Number(result[0]))) {
                resultB = true;
                break;
            }
        }
        return resultB;
    }
    ParseMarginOrPadding() {

    }
    ParseMarginOrPaddingCorner() {

    }
    CheckJSCallbackInfo(jsValue: any, result: number) {
        if (jsValue && typeof jsValue === 'object') {
            return true;
        }
        console.log('not object !');
        return false;
    }

    // JsWidth() {

    // }
    // JsHeight() {

    // }
}